﻿using FictionsDownload.BLL;
using FictionsDownload.Model;
using FictionsDownload.Static;
using FictionsDownload.UI.PopDialog;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace FictionsDownload.UI
{
    public enum EditState
    {
        新建 = 1,
        修改,
    }
    public partial class FormBookEdit : Form
    {
        #region 变量
        private EditState State;
        private BookModel bookModel = new BookModel();
        private Book book = new Book();
        private WebFormat webFormat = new WebFormat();
        private SortOrder SortOrder
        {
            get
            {
                foreach (RadioButton item in grBox_Sort.Controls)
                {
                    if (item.Checked)
                    {
                        if (item.Text == "升序")
                        {
                            return SortOrder.Ascending;
                        }
                        else if (item.Text == "降序")
                        {
                            return SortOrder.Descending;
                        }
                        else
                        {
                            return SortOrder.None;
                        }
                    }
                }
                return SortOrder.None;
            }
            set
            {
                string text;
                if( value== SortOrder.Ascending)
                {
                    text = "升序";
                }
                else if (value == SortOrder.Descending)
                {
                    text = "降序";
                }
                else
                {
                    text = "不排序";
                }
                foreach (RadioButton item in grBox_Sort.Controls)
                {
                    if (item.Text == text)
                    {
                        item.Checked = true;
                    }
                    else
                    {
                        item.Checked = false;
                    }
                }
            }
        }
        #endregion
        #region 构造初始化
        /// <summary>
        /// 新增或修改Book
        /// </summary>
        /// <param name="state">新增或者修改</param>
        /// <param name="AddFolderIdOrModBookId">修改时为BookId;新增时为FolderId</param>
        public FormBookEdit(EditState state,string AddFolderIdOrModBookId)
        {
            InitializeComponent();
            //属性、变量初始赋值
            txt_Url.Text = "http://www.";
            //修改、新增区分
            if (state == EditState.修改)
            {
                ModInit(AddFolderIdOrModBookId);
            }
            else
            {
                AddInit(AddFolderIdOrModBookId);
            }
        }
        private void AddInit(string folderId)
        {
            State = EditState.新建;
            cbox_BookMenusInit(folderId);
        }
        private void ModInit(string id)
        {
            State = EditState.修改;
            bookModel = book.Get<BookModel>(b => b.Id == id);
            txt_Title.Text = bookModel.Name;
            txt_Url.Text = bookModel.Url;
            txtDirectoryUrl.Text = bookModel.DirectoryUrl;
            txt_HtmlHead.Text = bookModel.HtmlHead;
            txt_HtmlTail.Text = bookModel.HtmlTail;
            txt_TextHead.Text = bookModel.TextHead;
            txt_TextTail.Text = bookModel.TextTail;
            txt_VolumeHead.Text = bookModel.VolumeHead;
            txt_VolumeTail.Text = bookModel.VolumeTail;
            ChbState.Checked = (bookModel.State == BookModel.EState.停止更新);
            this.SortOrder = bookModel.SortOrder;
            cbox_BookMenusInit(bookModel.FolderId);
        }
        private void cbox_BookMenusInit(string folderId)
        {
            cbox_BookMenus.DropDownStyle = ComboBoxStyle.DropDownList;
            var folders = book.GetList<FolderModel>().ToDictionary(f=>f.Id,f=>f.Name);
            cbox_BookMenus.DisplayMember = "Value";
            cbox_BookMenus.ValueMember = "Key";
            cbox_BookMenus.DataSource = new BindingSource(folders,null);
            cbox_BookMenus.SelectedValue = folderId;
        }
        #endregion
        #region btn_Add事件
        private void btn_Add_Click(object sender, EventArgs e)
        {
            //开始赋值
            bookModel.Name = txt_Title.Text;
            bookModel.Url = txt_Url.Text;
            bookModel.FolderId = cbox_BookMenus.SelectedValue.ToString();
            bookModel.DirectoryUrl = txtDirectoryUrl.Text;
            bookModel.HtmlHead = txt_HtmlHead.Text;
            bookModel.HtmlTail = txt_HtmlTail.Text;
            bookModel.TextHead = txt_TextHead.Text;
            bookModel.TextTail = txt_TextTail.Text;
            bookModel.VolumeHead = txt_VolumeHead.Text;
            bookModel.VolumeTail = txt_VolumeTail.Text;
            bookModel.SortOrder = this.SortOrder;
            if (ChbState.Checked)
            {
                bookModel.State = BookModel.EState.停止更新;
            }
            else
            {
                bookModel.State = BookModel.EState.正常更新;
            }
            //开始添加或者修改
            bool bl;
            if (State == EditState.新建)
            {
                bl = book.Add(bookModel);
            }
            else
            {
                bl = book.Update(bookModel);
            }
            if (bl)
            {
                book.SaveChanges();
                Dispose();
            }
            else
            {
                MessageBox.Show(book.Message);
            }
        }
        #endregion

        private void btnGetFormat_Click(object sender, EventArgs e)
        {
            string selectUrl = string.IsNullOrEmpty(txtDirectoryUrl.Text)? txt_Url.Text: txtDirectoryUrl.Text;
            WebFormatModel webFormatModel = webFormat.FindByUrlDns(selectUrl);
            txt_HtmlHead.Text = webFormatModel?.HtmlHead;
            txt_HtmlTail.Text = webFormatModel?.HtmlTail;
            txt_TextHead.Text = webFormatModel?.TextHead;
            txt_TextTail.Text = webFormatModel?.TextTail;
            txt_VolumeHead.Text = webFormatModel?.VolumeHead;
            txt_VolumeTail.Text = webFormatModel?.VolumeTail;
            this.SortOrder = webFormatModel?.SortOrder ?? SortOrder.None;
        }
        private void btnSaveFormat_Click(object sender, EventArgs e)
        {
            string selectUrl = string.IsNullOrEmpty(txtDirectoryUrl.Text) ? txt_Url.Text : txtDirectoryUrl.Text;
            WebFormatModel webFormatModel = webFormat.FindByUrlDns(selectUrl) ?? new WebFormatModel();
            webFormatModel.HtmlHead = txt_HtmlHead.Text;
            webFormatModel.HtmlTail = txt_HtmlTail.Text;
            webFormatModel.TextHead = txt_TextHead.Text;
            webFormatModel.TextTail = txt_TextTail.Text;
            webFormatModel.VolumeHead = txt_VolumeHead.Text;
            webFormatModel.VolumeTail = txt_VolumeTail.Text;
            webFormatModel.SortOrder = this.SortOrder;
            if (webFormatModel.AutoId == 0)
            {
                webFormatModel.Name = new Uri(selectUrl).Authority;
                webFormatModel.Url = "https://" + new Uri(selectUrl).Authority;
                webFormat.Insert(webFormatModel);
            }
            else
            {
                webFormat.Update(webFormatModel);
            }
            webFormat.SaveChanges();
        }
        private void BtnFilterDirectory_MouseClick(object sender, MouseEventArgs e)
        {
            if (bookModel == null || bookModel.AutoId == 0)
            {
                MessageBox.Show("添加书籍时，无法查看过滤章节");
                return;
            }
            FormFilterDirectory formFilterDirectory = new FormFilterDirectory(bookModel.Id);
            formFilterDirectory.Show();
        }

        private void txtDirectoryUrl_TextChanged(object sender, EventArgs e)
        {
            string selectUrl = txtDirectoryUrl.Text;
            WebFormatModel webFormatModel = webFormat.FindByUrlDns(selectUrl);
            txt_HtmlHead.Text = webFormatModel?.HtmlHead;
            txt_HtmlTail.Text = webFormatModel?.HtmlTail;
            txt_TextHead.Text = webFormatModel?.TextHead;
            txt_TextTail.Text = webFormatModel?.TextTail;
            txt_VolumeHead.Text = webFormatModel?.VolumeHead;
            txt_VolumeTail.Text = webFormatModel?.VolumeTail;
            this.SortOrder = webFormatModel?.SortOrder ?? SortOrder.None;
        }
    }
}
